﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.ThangDanhGiaService;
namespace eTMS.Controllers{
public class ThangDanhGiaControllerBase : CommonController{
	public ThangDanhGiaControllerBase()
		: base()
	{	
		this.dao = new ThangDanhGiaServiceClient();
	}
	~ThangDanhGiaControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private ThangDanhGiaEntity oThangDanhGia;	
	public ThangDanhGiaEntity ThangDanhGiaObject{
		get{
			return oThangDanhGia;
		}
		set{
			oThangDanhGia=value;
		}
	}
	protected ThangDanhGiaServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSThangDanhGia(false);			
			return _Table;
		}
	}
	public void LoadAllDSThangDanhGia(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oThangDanhGia==null)return false;
		bool result=true;
		try{
			oThangDanhGia = dao.SaveRet(oThangDanhGia);
		}
		catch{
			result=false;
		}
		if(result){
			_ListThangDanhGia.Add(oThangDanhGia);
		}
		return result;
	}
	public bool Update(){
		if(oThangDanhGia==null)return false;
		return dao.Update(oThangDanhGia);
	}
	public bool Delete(){
		if(oThangDanhGia==null)return false;
		bool retvalue = dao.Delete(oThangDanhGia);
		if(retvalue){			
			_ListThangDanhGia.Remove(oThangDanhGia);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 ThangDanhGia_ID){
		bool retvalue = dao.DeleteByID(ThangDanhGia_ID);		
		if(retvalue){
			ThangDanhGiaEntity oDeleted=FindThangDanhGia(ThangDanhGia_ID);
			if(oDeleted!=null){
				_ListThangDanhGia.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oThangDanhGia = new ThangDanhGiaEntity();
	}
    //Copy data from data row in grid to object
    public ThangDanhGiaEntity MapDataRowToObject(DataRow row){
		ThangDanhGiaEntity o = new ThangDanhGiaEntity();
		 o.TenThangDanhGia=Convert.ToString(row["TenThangDanhGia"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("ThangDanhGia_ID"))
		    o.ThangDanhGia_ID=(Int32)(row["ThangDanhGia_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(ThangDanhGiaEntity o,DataRow row){
		row["TenThangDanhGia"]=o.TenThangDanhGia;
		row["GhiChu"]=o.GhiChu;
		row["ThangDanhGia_ID"]=o.ThangDanhGia_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.ThangDanhGiaObject,row);
    }
	public void LoadObject(Int32 ThangDanhGia_ID){
		ThangDanhGiaEntity oLoad=FindThangDanhGia(ThangDanhGia_ID);
		if(oLoad==null){
			oThangDanhGia=this.LoadThangDanhGia(ThangDanhGia_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oThangDanhGia.IsNullThangDanhGia_ID;
			if(!bNotIsNull){
				this.ListThangDanhGia.Add(oThangDanhGia);
			}
		}
		else{
			oThangDanhGia=oLoad;
		}
	}
	#region CachedList ThangDanhGia
	private static List<ThangDanhGiaEntity> _ListThangDanhGia=new List<ThangDanhGiaEntity>();
    public List<ThangDanhGiaEntity>ListThangDanhGia{
		get{
			return _ListThangDanhGia;
		}
	}
	public static List<ThangDanhGiaEntity>ListThangDanhGiaStatic{
		get{
			return _ListThangDanhGia;
		}
	}
	public ThangDanhGiaEntity FindThangDanhGia(Int32 ThangDanhGia_ID){
		ThangDanhGiaEntity oResult=this.ListThangDanhGia.Find(delegate(ThangDanhGiaEntity oFind){
			bool result=true; 
			result&=oFind.ThangDanhGia_ID.Equals(ThangDanhGia_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllThangDanhGia(bool clearCache){
		if(_ListThangDanhGia.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		ThangDanhGiaServiceClient dao = new ThangDanhGiaServiceClient();
		_ListThangDanhGia=new List<ThangDanhGiaEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public ThangDanhGiaEntity LoadThangDanhGia(Int32 ThangDanhGia_ID){			
		ThangDanhGiaEntity o=dao.SelectOne(ThangDanhGia_ID);		
		return o;
	}
	#endregion
}
}
